<script setup>
import { ref } from 'vue'
import { uploadClick, uploadChange, dragover, dragleave, drop, submitUpload } from '../js/uploadFile'
import { mainStore } from '../../../store/mainStore'
import { goback } from '../js/goBack.js'

const store = mainStore()

let showChildren = ref(false)

let historyArr = ref([])
historyArr.value = JSON.parse(localStorage.getItem('historyInputs')) ?? []

let errTip = ref(false)
const uploadPath = (value) => {
  const reg = /^\/([^\/\u4e00-\u9fa5]+\/)*[^\/\u4e00-\u9fa5]+\/$/
  if (!reg.test(value)) {
    errTip.value = true
  } else {
    errTip.value = false
  }
}

const inputPath = (path) => {
  store.uploadFile.path = path
  uploadPath(path)
  showChildren.value = false
}

const handleChange = () => {
  showChildren.value = !showChildren.value
  historyArr.value = JSON.parse(localStorage.getItem('historyInputs')) ?? ['/home/airos/param/', '/home/airos/log/']
}

const deleteHistory = (e, path) => {
  e.stopPropagation()
  let res = JSON.parse(localStorage.getItem('historyInputs'))
  res = res.filter((item) => item != path)
  historyArr.value = res
  localStorage.setItem('historyInputs', JSON.stringify(res))
}
</script>

<template>
  <div class="upload-container">
    <div class="title">文件信息</div>

    <div class="showInputEle">
      <p>文件名称：</p>
      <el-input v-model="store.uploadFile.name" style="width: 240px" disabled placeholder="无准备上传文件" />
    </div>

    <div class="showUploadBox">
      <p>传输文件：</p>
      <div class="uploadBox" @dragover="dragover" @dragleave="dragleave" @drop="drop">
        <div id="drop-area">
          <div v-if="store.uploadFile.name" class="showFile">
            <p>当前选中文件为：</p>
            <p>{{ store.uploadFile.name }}</p>
            <span @click="uploadClick">重新上传 / 拖拽</span>
          </div>
          <div v-else>
            <span @click="uploadClick">点击上传 /</span>
            <span> 拖拽到此区域</span>
          </div>
        </div>
        <input type="file" @change="uploadChange" id="file-input" style="display: none" />
      </div>
    </div>

    <div class="sizetip">*文件的传输大小不得超过4GB</div>

    <div class="showInputEle">
      <p>传输路径：</p>
      <div>
        <el-input @input="uploadPath" v-model="store.uploadFile.path" style="width: 240px" placeholder="输入路径" />

        <div class="img" @click="handleChange">
          <img :class="showChildren ? 'up' : ''" src="/public/global/showDown.png" />
        </div>

        <div class="showChildren" v-if="showChildren">
          <div v-for="item in historyArr" :key="item" @click="inputPath(item)">
            <p>{{ item }}</p>
            <img src="/public/global/close_1.png" @click="deleteHistory($event, item)" />
          </div>
        </div>

        <div class="tip" v-show="errTip">请输入正确的路径,例如: /test/test1/test2/test/</div>
      </div>
    </div>

    <div class="btns">
      <div class="btn_bd" @click="goback">取消</div>
      <div class="btn_bg" @click="submitUpload(errTip)">确定</div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.upload-container {
  width: 100%;
  height: 100%;
  font-size: 14px;
  flex-shrink: 0;
  position: relative;
  > div {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    > p {
      margin-right: 12px;
    }
  }
}

.title {
  font-size: 14px;
  font-weight: 700;
  height: 36px;
  line-height: 36px;
  // margin-top: 12px;
  margin-bottom: 12px;
}

.showUploadBox {
  align-items: start !important;
}

.showInputEle {
  width: auto;
  > div {
    width: 240px;
    height: 100%;
    position: relative;
    display: flex;
    .img {
      position: absolute;
      top: 0px;
      right: 6px;
      width: 40px;
      height: 26px;
      cursor: pointer;
      border-left: 1px solid #ccc;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        width: 20px;
        height: 20px;
      }
    }
    .showChildren {
      width: 100%;
      height: 180px;
      border: 1px solid #ccc;
      position: absolute;
      top: 30px;

      > div {
        height: 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 10px;
        margin-top: 6px;
        margin-bottom: 6px;
        p {
          max-width: 230px;
          overflow: hidden;
          line-height: 20px;
        }
        img {
          width: 18px;
          height: 18px;
          cursor: pointer;
        }
      }
      > div:hover {
        background-color: rgba(64, 149, 229, 0.4);
      }
    }
  }
}

.tip {
  font-size: 12px;
  position: absolute;
  left: 260px;
  width: 300px;
  height: 26px;
  line-height: 26px;
  color: red;
}

.sizetip {
  font-size: 12px;
  margin-top: -14px;
  width: 500px;
  padding-left: 82px;
  opacity: 0.5;
}

.up {
  transform: rotate(180deg);
  transition: transform 0.3s ease;
}

.uploadBox {
  width: 240px;
  height: 102px;
  font-size: 12px;
}

#drop-area {
  border: 1px solid #ccc;
  width: 100%;
  height: 100%;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  > div {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;

    > span:nth-child(1) {
      color: blue;
      cursor: pointer;
    }
  }
  .showFile {
    flex-direction: column;
    align-content: flex-start;
    position: absolute;
    top: 0;
    z-index: 2;
    pointer-events: none;
    > p {
      width: 100%;
      text-align: left;
      padding: 0 10px;
      color: rgba(64, 149, 229, 1);
    }
    p:nth-child(2) {
      margin-top: 10px;
    }
    > span {
      position: absolute;
      bottom: 4px;
      right: 4px;
      color: blue;
      pointer-events: auto;
      cursor: pointer;
    }
  }
}

#drop-area.highlight {
  border-color: purple;
}

.btns {
  width: 100%;
  position: absolute;
  bottom: 120px;
  display: flex;
  justify-content: center;
  > div {
    padding: 4px 12px;
    border-radius: 4px;
    margin-left: 40px;
    cursor: pointer;
  }
}
</style>

<style>
.el-select--small .el-select__wrapper {
  min-height: 26px;
}
</style>
